/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package HumanCentredSystemDesign;

import java.util.ArrayList;

/**
 *
 * @author Martin
 */
public class Copy {
    private String deweyIndex;
    private Boolean referenceOnly;
    private String itemIdentifier;
            
    public Copy(){}
    public Copy(String _itemIdentifier, String _deweyIndex, Boolean _referenceOnly){
        this.deweyIndex = _deweyIndex;
        this.itemIdentifier = _itemIdentifier;
        this.referenceOnly = _referenceOnly;
        
    }
    
    public String getDeweyIndex(){
        return this.deweyIndex;
    }
    public void setDeweyIndex(String _deweyIndex){
        this.deweyIndex = _deweyIndex;
    }
    public Boolean isReferenceOnly(){
        return this.referenceOnly;
    }
    public void setReferenceOnly(Boolean _referenceOnly){
        this.referenceOnly = _referenceOnly;
    }
    public String getItemIdentifier(){
        return this.itemIdentifier;
    }
    public void setItemIdentifier(String _itemIdentifier){
        this.itemIdentifier = _itemIdentifier;
    }

  
    @Override
    public String toString(){
        return " "+ deweyIndex +" Reference Only: "+ referenceOnly +" Item identifier: " + itemIdentifier;
    }
    public boolean onLoan(){
        //Connect to database to find if this Copy is on loan
        Connect myConn = new Connect();
        myConn.establishConnection();
        ArrayList<Loan> myLoans = myConn.getLoansFromDatabase("", this.deweyIndex);
        myConn.closeConnection();
        return  !(myLoans.isEmpty());
    }
    public Loan getLoanDetails(){
        try{
            Connect myConn = new Connect();
            myConn.establishConnection();        
            Loan myLoan = myConn.getLoansFromDatabase("", deweyIndex).get(0);
            myConn.closeConnection();
            return myLoan;
        }
        catch(Exception err){
            System.out.println("Error getting loan details from Copy: " +  err.toString());
            return null;
        }
    }
    public Item getLoanedItem(){
        ArrayList<Item> reservedItems;
        Connect myConn = new Connect();
        myConn.establishConnection();
        
        try
        {
            reservedItems = myConn.getBooksFromDatabase(itemIdentifier, "", "", "", "");
        } 
        catch(Exception err)
        {
            System.out.println("Cannot get Books Associated with this reservation: "  + err.toString());
            reservedItems = null;
        }

        try
        {
            ArrayList<Item> reservedP = myConn.getPeriodicalsFromDatabase(itemIdentifier, "", "", "", "", "");
            for (Item i : reservedP)
            {
                reservedItems.add(i);
            }
        }
        catch(Exception err)
        {
            System.out.println("Cannot get Periodicals Associated with this Reservation: " + err.toString());    
        }
     

        try{
            myConn.closeConnection();
            return reservedItems.get(0) ;
            
        }
        catch(Exception err)
        {
            System.out.println("There are no items associated with the ID: " + err.toString());
            myConn.closeConnection();
            return null; 
        }
    }
    
}
